package cn.group.controller;

import cn.group.domain.Classes;
import cn.group.domain.Faculty;
import cn.group.domain.SC;
import cn.group.service.FacultyService;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/faculty")
public class FacultyController {
    @Autowired
    private FacultyService facultyService;
    @GetMapping("/showFaculty")
    public  String getFacultyShow(){
        return "faculty";
    }
    @RequestMapping("/getFacultyList")
    @ResponseBody
    public Map<String,Object>getFacultyList(){
        List<Faculty> facultyList=facultyService.queryAllFaculty();
        System.out.println(facultyList);
        Map<String,Object> result=new HashMap<>();
        result.put("code",0);
        result.put("facultyList",facultyList);
        result.put("message","获取班级信息成功");
        return result;
    }
    @RequestMapping("/getListByPage")
    @ResponseBody
    public Map<String,Object> getListByPage(Integer currentPage,Integer pageSize){
        PageInfo<Faculty> pageInfo=facultyService.getFacultyByPage(currentPage,pageSize);
        System.out.println(pageInfo.getList());

        Map<String,Object>result=new HashMap<>();
        result.put("code",0);
        result.put("FacultyList",pageInfo.getList());
        result.put("totalPage",pageInfo.getPages());
        result.put("message","获取用户数据成功");
        return result;
    }
    @RequestMapping("/getOneFaculty")
    @ResponseBody
    public  Map<String,Object>getOneFaculty(Integer id){
        Faculty faculty=facultyService.queryAllById(id);
        Map<String,Object>result=new HashMap<>();
        if (faculty==null){
            result.put("code",-1);
            result.put("message","获取学院信息失败");
        }else {
            result.put("code",0);
            result.put("message","获取用户信息成功");
            result.put("FacultyList",faculty);
        }
        return result;
    }
    @RequestMapping("/ChangeOne")
    @ResponseBody
    public  Map<String,Object>ChangeOne(Faculty faculty){
        Boolean bl=facultyService.modifyOne(faculty);
        Map<String,Object>result=new HashMap<>();
        if (bl==true){
            result.put("code",0);
            result.put("message","编辑用户成功");
        }else if(bl==false){
            facultyService.insertOneFaculty(faculty);
            result.put("code",0);
            result.put("message","添加用户成功");
        }else {
            result.put("code",-1);
            result.put("message","操作失败");
        }
        return result;
    }

    @RequestMapping("/deleteOne")
    @ResponseBody
    public  Map<String,Object>deleteOne(Faculty faculty){
        boolean bl=facultyService.deleteOne(faculty);
        Map<String ,Object>result=new HashMap<>();
        if (bl==true){
            result.put("code",0);
            result.put("message","删除成功");
        }else {
            result.put("code",-1);
            result.put("message","删除失败");
        }
        return result;
    }
}
